package api

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"mogutouERP/models"
	"mogutouERP/utils/response"
)

// CreateCommodity 创建新商品类型
func CreateCommodity(c *gin.Context) {
	var data struct {
		ID                     string  `json:"id" binding:"required"`
		No                     string  `json:"no" binding:"required"`
		CommodityType          string  `json:"type"`
		Name                   string  `json:"name" binding:"required"`
		Colour                 string  `json:"colour"`
		MerchandiseSafetyStock uint    `json:"merchandiseSafetyStock"`
		PresaleNumber          uint    `json:"presaleNumber"`
		PackgeNum              uint    `json:"packgeNum" `
		Weight                 float32 `json:"weight" `
		PackgeWeight           float32 `json:"packgeWeight"`
		PackgeCube             string  `json:"packgeCube"`
		Remark                 string  `json:"remark"`
		Supply                 string  `json:"supply" binding:"required"`
		Location               string  `json:"location" binding:"required"`
		Size                   string  `json:"size"`
		PurchasePrice          float32 `json:"purchase_price"`
		Ponents                []struct {
			ID     string `json:"id" `
			Number uint   `json:"number" `
		}
	}
	err := c.ShouldBindJSON(&data)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
		return
	}
	ponents := make([]models.CommodityComponents, len(data.Ponents))
	if len(data.Ponents) != 0 {
		for i, v := range data.Ponents {
			ponents[i].ComponentID = v.ID
			ponents[i].Number = v.Number
		}
	}

	commodity := models.Commodity{
		ID:                     data.ID,
		No:                     data.No,
		CommodityType:          data.CommodityType,
		Name:                   data.Name,
		Colour:                 data.Colour,
		MerchandiseSafetyStock: data.MerchandiseSafetyStock,
		PresaleNumber:          data.PresaleNumber,
		PackgeNum:              data.PackgeNum,
		Weight:                 data.Weight,
		PackgeWeight:           data.PackgeWeight,
		PackgeCube:             data.PackgeCube,
		Remark:                 data.Remark,
		Supply:                 data.Supply,
		Location:               data.Location,
		Size:                   data.Size,
		PurchasePrice:          data.PurchasePrice,
	}
	err = models.CreateCommodity(&commodity, ponents)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusCreated, models.GetCommodity(data.ID))
}

// UpdateCommodity 更新商品信息
func UpdateCommodity(c *gin.Context) {
	commodityID := c.Param("id")

	if !models.HaveCommodity(commodityID) {
		c.AbortWithStatusJSON(http.StatusNotFound, response.Error{Error: "没有此商品信息"})
		return
	}

	var data struct {
		No                     string  `json:"no"`
		Name                   string  `json:"name"`
		CommodityType          string  `json:"type"`
		Colour                 string  `json:"colour"`
		MerchandiseSafetyStock uint    `json:"merchandiseSafetyStock"`
		PresaleNumber          uint    `json:"presaleNumber"`
		PackgeNum              uint    `json:"packgeNum"`
		Weight                 float32 `json:"weight"`
		PackgeCube             string  `json:"packgeCube"`
		PackgeWeight           float32 `json:"packgeWeight"`
		Logo                   string  `json:"logo"`
		Remark                 string  `json:"remark"`
		Location               string  `json:"location"`
		Size                   string  `json:"size"`
		PurchasePrice          float32 `json:"purchase_price"`
		Supply                 string  `json:"supply"`
		Ponents                []struct {
			ID     string `json:"id" `
			Number uint   `json:"number" `
		}
	}
	err := c.ShouldBindJSON(&data)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
		return
	}
	ponents := make([]models.CommodityComponents, len(data.Ponents))
	if len(data.Ponents) != 0 {
		for i, v := range data.Ponents {
			ponents[i].ComponentID = v.ID
			ponents[i].Number = v.Number
		}
	}
	commodity := models.Commodity{
		Name:                   data.Name,
		No:                     data.No,
		CommodityType:          data.CommodityType,
		Colour:                 data.Colour,
		MerchandiseSafetyStock: data.MerchandiseSafetyStock,
		PresaleNumber:          data.PresaleNumber,
		PackgeNum:              data.PackgeNum,
		Weight:                 data.Weight,
		PackgeWeight:           data.PackgeWeight,
		PackgeCube:             data.PackgeCube,
		Logo:                   data.Logo,
		Remark:                 data.Remark,
		Location:               data.Location,
		Size:                   data.Size,
		Supply:                 data.Supply,
		PurchasePrice:          data.PurchasePrice,
	}
	err = models.UpdateCommodity(commodityID, &commodity, ponents)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusCreated, gin.H{})
}

// DeleteCommodity 删除商品
func DeleteCommodity(c *gin.Context) {
	commodityID := c.Param("id")

	if !models.HaveCommodity(commodityID) {
		c.AbortWithStatusJSON(http.StatusNotFound, response.Error{Error: "没有此商品信息"})
		return
	}

	err := models.DeleteCommodity(commodityID)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusNoContent, gin.H{})
}

// ListCommodities 列举所有商品
func ListCommodities(c *gin.Context) {
	commodities, err := models.GetCommodities()
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusOK, commodities)
}

// ListCommodities 列举所有商品,含库存量
func ListCommoditiesStock(c *gin.Context) {
	commodities, err := models.GetCommoditiesStock()
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusOK, commodities)
}

// ListCommoditiesAsAdmin list所有商品信息
func ListCommoditiesAsAdmin(c *gin.Context) {
	commodities, err := models.GetCommoditiesWithPurchasePrice()
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusOK, commodities)
}

// ListCommodityType list所有商品类型
func ListCommodityType(c *gin.Context) {
	commodityType, err := models.GetCommodityType()
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusOK, commodityType)
}

// GetLocation list所有位置
func GetLocation(c *gin.Context) {
	locations, err := models.GetLocation()
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusOK, locations)
}
